package com.lee.crawler.gov.strategy.list;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.List;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;

import com.lee.crawler.gov.strategy.GlArticleStrategy;

@Component
public class GlListStrategy implements ListStrategy {
	@Autowired
	private GlArticleStrategy glArticleStrategy;

	
	public static void main(String[] args) throws UnsupportedEncodingException {

		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/applicationContext.xml",
				"spring/applicationContext-datasource.xml", "spring/applicationContext-redis.xml");
		context.start();
		Arrays.asList(context.getBeanDefinitionNames()).forEach(System.out::println);

		GlListStrategy bean = (GlListStrategy) context.getBean("glListStrategy");
		bean.saveCrawlerArticles();

		try {
			System.in.read();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (context != null) {
				context.close();
			}
		}

	}
	
	
	@Override
	public String setCategroyUrl() {
		return "http://www.gl.gov.cn/News/ShowArticle.aspx?cid=4416";
	}

	@Override
	public void saveCrawlerArticles() {
		savePage(setCategroyUrl());
	}
	
	private void savePage(String pageUrl) {
		Document contentStr = getContent(pageUrl);
		List<String> links = contentStr.select("div.ArticleWrap li a").eachAttr("abs:href");
		links.stream().forEach(link -> {
			try {
				glArticleStrategy.saveCrawlerArticle(link);
			} catch (Exception e) {
				e.printStackTrace();
			}
		});
		
		Element nextPageEl = contentStr.selectFirst(".ArticleWrapPage a:contains(下页)");
		if(nextPageEl!=null){
			String absUrl = nextPageEl.absUrl("href");
			if(absUrl.matches("^http.*")){
				savePage(absUrl);
			}
		}
		
		
	}

}
